package suzf.top.kotlinjava.common.utils

import com.baomidou.mybatisplus.core.toolkit.StringPool
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig
import com.baomidou.mybatisplus.generator.config.*
import com.baomidou.mybatisplus.generator.config.po.TableInfo
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.aspectj.weaver.tools.cache.SimpleCacheFactory.path



/**
 *
 *========================
 * Created with IntelliJ IDEA
 * Author:Mr.Su
 * Date:2019/9/26
 * Time:11:51
 * ========================
 */
fun main(args: Array<String>) {

    val mpg = AutoGenerator()
    val gc = GlobalConfig()
    val projectPath = System.getProperty("user.dir")
    println("projectPath的路径：${projectPath}")
    println("代码生成的路径：${projectPath}/src/main/kotlin")
    gc.isFileOverride = true
    gc.outputDir = "$projectPath/src/main/kotlin"
    gc.author = "Mr.Su"
    gc.isOpen = false
    // 自定义文件命名，注意 %s 会自动填充表实体属性！
    gc.controllerName = "%sController"
    gc.serviceName = "%sService"
    gc.serviceImplName = "%sServiceImpl"
    gc.mapperName = "%sMapper"
    gc.xmlName = "%sMapper"
    gc.isKotlin = true
    gc.isBaseResultMap = true
    gc.isBaseColumnList = true
    mpg.globalConfig = gc

    //  使用自定义的模板文件
    val templateConfig = TemplateConfig()
    templateConfig.controller = "templates/controller.java"
    templateConfig.entityKt = "templates/entity.kt"
    templateConfig.mapper = "templates/mapper.java"
    templateConfig.xml = "templates/mapper.xml"
    templateConfig.service = "templates/service.java"
    templateConfig.serviceImpl = "templates/serviceImpl.java"
    mpg.template = templateConfig

    // 数据源配置
    val dsc = DataSourceConfig()
    dsc.url = "jdbc:mysql://localhost:3306/ykpushdata?useUnicode=true&useSSL=false&characterEncoding=utf8"
    // dsc.setSchemaName("public");
    dsc.driverName = "com.mysql.jdbc.Driver"
    dsc.username = "root"
    dsc.password = "root"
    mpg.dataSource = dsc

    // 策略配置
    var strategy = StrategyConfig()
    // strategy.setTablePrefix(new String[] { "sys_" });// 此处可以修改为您的表前缀
    strategy.naming = NamingStrategy.underline_to_camel// 表名生成策略
    strategy.setInclude("user","role")

    strategy.superServiceClass = null
    strategy.superServiceImplClass = null
    strategy.superMapperClass = null
    mpg.strategy = strategy

    // 包配置
    val pc = PackageConfig()
    pc.parent = "suzf.top.kotlinjava"
    pc.controller = "service.controller"
    pc.service = "service.service"
    pc.serviceImpl = "service.service.impl"
    pc.mapper = "common.mapper"
    pc.entity = "common.entity"
    pc.xml = "common.mapper.xml"
    mpg.packageInfo = pc

    //初始化自定义配置
    val cfg = object : InjectionConfig() {
        override fun initMap() {

        }
    }
    val xmlPath = "templates/mapper.xml.vm"
    val focList = ArrayList<FileOutConfig>()
    focList.add(object: FileOutConfig(xmlPath) {
        override fun outputFile(tableInfo: TableInfo?): String {
            return (projectPath + "/src/main/resources/mapper/" + tableInfo!!.entityName + "Mapper"
                    + StringPool.DOT_XML)
        }
    })
    cfg.fileOutConfigList = focList
    mpg.cfg = cfg

    // 执行生成
    mpg.execute()
}
